package com.sojson.project.filedepot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.project.filedepot.entity.dto.FileFieldDto;
import com.sojson.project.filedepot.entity.dto.FileFieldPageDto;
import com.sojson.project.filedepot.entity.po.FileField;
import com.sojson.project.filedepot.entity.vo.FileFieldVo;
import com.sojson.project.filedepot.service.FileFieldService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件库字段表控制器
 * 
 * @author liu
 * @date 2022-10-27
 */
@Api(tags = "文件库字段表管理")
@RestController
@RequestMapping("/depot/field")
public class FileFieldController {

    @Autowired
    private FileFieldService fileFieldService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/depot/field/save')")
    @ApiOperation("给文件库字段表添加一条数据")
    public Result<Boolean> save(@RequestBody FileField dto) {
        return Result.success(fileFieldService.save(dto));
    }

    /**
     * 列表查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/depot/field/list')")
    @ApiOperation(value = "查询文件库字段表数据", response = FileFieldVo.class)
    public Result<List<FileFieldVo>> list(FileFieldDto dto) {
        return Result.success(fileFieldService.list(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/depot/field/page')")
    @ApiOperation(value = "分页查询文件库字段表数据", response = FileFieldVo.class)
    public Result<List<FileFieldVo>> page(FileFieldPageDto dto) {
        return Result.success(fileFieldService.page(dto));
    }

    /**
     * 清空数据
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/depot/field/clear')")
    @ApiOperation("清空文件库字段表数据")
    public Result<Boolean> clear() {
        return Result.success(fileFieldService.clear());
    }

    /**
     * 通过id删除
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("@ss.hasPermi('/depot/field/remove/{id}')")
    @ApiOperation("通过id删除文件库字段表数据")
    public Result<Boolean> remove(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(fileFieldService.removeById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/depot/field/remove')")
    @ApiOperation("通过id列表删除文件库字段表数据")
    public Result<Boolean> remove(@RequestBody FileFieldDto dto) {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(fileFieldService.removeByIds(ids));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/depot/field/updateById')")
    @ApiOperation("通过id更新文件库字段表数据")
    public Result<Boolean> updateById(@RequestBody FileField dto) {
        return Result.success(fileFieldService.updateById(dto));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/depot/field/info/{id}')")
    @ApiOperation(value = "通过id查询文件库字段表数据", response = FileFieldVo.class)
    public Result<FileFieldVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(fileFieldService.findById(id));
    }

}